package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.Coach;
import com.example.wjh.entity.Player;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.CoachMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教练
 * 后端接口
 * @author
 * @email
 * @date 2022-07-31 07:01:29
 */
@CrossOrigin
@RestController
@RequestMapping("/coach")
public class CoachController {

    @Autowired
    private CoachMapper CoachMapper;

    //按用户名搜索
    @GetMapping("/info/{username}")
    public Result getCoachInfoByUsername(@PathVariable("username") String username) {
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
       Coach coach= CoachMapper.selectOne(queryWrapper);
        if (coach != null) {
            return Result.success(coach);
        } else {
            return Result.error("Coach not found for username: " + username);
        }
    }


    //图表三个值
    @GetMapping("/maingong")
    public int getcoachmaingong(){   //进攻为主
        return CoachMapper.findmaingong();
    }
    @GetMapping("/mainfang")
    public int getcoachmainfang(){   //防守为主
        return CoachMapper.findmainfang();
    }
    @GetMapping("/gongfang")
    public int getcoachgongfang(){   //攻防平衡
        return CoachMapper.findgongfang();
    }

    //计算教练数量
    @GetMapping("/count")
    public Result getCoachCount() {
        long count = CoachMapper.selectCount(null);
        return Result.success(count);
    }

    /**
     * 查询
     * @return
     */
    @GetMapping()
    public Result getCoachs(){  return Result.success(CoachMapper.selectList(null));}

    /**
     * 添加
     * @param Coach
     * @return
     */
    @PostMapping()
    public Result createCoach(@RequestBody Coach Coach) {
        CoachMapper.insert(Coach);
        return Result.success();
    }

    /**
     * 更新
     * @param id
     * @param Coach
     * @return
     */
    @PutMapping("/{id}")
    public Result updateCoach(@PathVariable("id") int id, @RequestBody Coach Coach) {
        Coach existingCoach = CoachMapper.selectById(id);
        if (existingCoach != null) {
            Coach.setId(id);
            CoachMapper.updateById(Coach);
            return Result.success();
        } else {
            return null;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delCoach(@PathVariable("id") int id) {
        CoachMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delCoachBatch(@RequestBody List<Long> ids) {
        CoachMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 模糊查询
     * @param name
     * @return
     */
    @GetMapping("/search")
    public Result searchPlayer(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String teachage,
            @RequestParam(required = false) String type
    ) {
        return Result.success(CoachMapper.searchCoach(name,username,teachage,type));
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getCoachList(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        Page<Coach> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = CoachMapper.selectPage(page,null);
        return Result.success(ipage);
    }
}
